#include <mosquitto.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "app_config.h"
#ifndef APPCFG_DEFAULT_MQTT_HOST
#  define APPCFG_DEFAULT_MQTT_HOST "mqtt.yyzlab.com.cn"
#endif
#ifndef APPCFG_DEFAULT_MQTT_PORT
#  define APPCFG_DEFAULT_MQTT_PORT 1883
#endif
#ifndef APPCFG_DEFAULT_PUB_TOPIC
#  define APPCFG_DEFAULT_PUB_TOPIC "1756687933516/Device2AIOTSIM"
#endif

int main()

{
    mosquitto_lib_init();
    struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
    if(mosq == NULL)
    {
        printf("Error: Out of memory.\n");
        return 1;
    }
    int error = mosquitto_connect(mosq, APPCFG_DEFAULT_MQTT_HOST, APPCFG_DEFAULT_MQTT_PORT, 60);
    if(error != MOSQ_ERR_SUCCESS)
    {
        printf("Error:%s\n",mosquitto_strerror(error));
        return 1;
    }
    printf("连接成功\n");
    //char* data ="{\"lamp\":true,\"id\":0}";
    //char* data = "{\"tem\":50,\"hum\":20,\"id\":0}";
    //char* data = "{\"PM2.5\":260,\"id\":0}";
    //char* data = "{\"tem\":250,\"id\":0}";
    //char* data = "{\"lamp\":true,\"tem\":50,\"hum\":20,\"PM2.5\":260,\"id\":0}";
    char* data = "{\"light\":1234,\"id\":0}";

    while(1)
    {
        error = mosquitto_publish(mosq, NULL, APPCFG_DEFAULT_PUB_TOPIC, (int)strlen(data), data, 2, false);
        if(error != MOSQ_ERR_SUCCESS)
        {
            printf("Error:%s\n",mosquitto_strerror(error));
            return 1;

        }
        sleep(1);
    }
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();
}
